function register() {
  const registerData = {
    email: registerForm.email.value,
    username: registerForm.username.value,
    password: registerForm.password.value
  }
  let btn = document.getElementsByName('submit')[0];
  btn.setAttribute('disabled','disabled');
  let checkRes = checkFormat(registerData.email, registerData.username, registerData.password, registerForm.passwordConfirm.value);
  if(checkRes != false) {
    request('http://175.178.51.126:8091/smallA/register', 'POST', JSON.stringify(registerData))
    .then(value => {
      if(value.code == 200) {
        alert('注册成功！');
        window.location.href = 'login.html';
      }else{
        alert(value.msg);
      }
    })
    .catch(error => {
      console.log(error);
    })
  }else {
    btn.removeAttribute('disabled');
  }
}
function login(method) {
  let loginData = {};
  let btn = document.getElementsByTagName('button');
  btn[0].setAttribute('disabled', 'disabled');
  btn[1].setAttribute('disabled', 'disabled');
  if(method == 'email') {
    loginData = {
      email: loginFormByEmail.email.value,
      password: loginFormByEmail.password.value
    }
  }else if(method == 'username') {
    loginData = {
      username: loginFormByUsername.username.value,
      password: loginFormByUsername.password.value
    }
  }
  let checkRes = checkFormat(loginData.email, loginData.username, loginData.password);
  if(checkRes != false) {
    request('http://175.178.51.126:8091/smallA/login', 'POST', JSON.stringify(loginData))
    .then(value => {
      if(value.code == 200) {
        if((loginFormByEmail.save.checked && method == 'email') || (loginFormByUsername.save.checked && method == 'username')) {
          for(let key in value.data) {
            setCookie(key, value.data[key], 15);
          }
        }else {
          for(let key in value.data) {
            window.sessionStorage.setItem(key, value.data[key]);
          }
        }
        alert('登录成功！');
        window.location.href = 'profile.html';
      }else{
        alert(value.msg);
        btn[0].removeAttribute('disabled');
        btn[1].removeAttribute('disabled');
      }
    })
    .catch(error => {
      console.log(error);
      btn[0].removeAttribute('disabled');
      btn[1].removeAttribute('disabled');
    })
  }else {
    btn[0].removeAttribute('disabled');
    btn[1].removeAttribute('disabled');
  }
}
function getProfileData() {
  if(sessionStorage.getItem('id') != null) {
    document.getElementById('id').innerHTML = sessionStorage.getItem('id');
    document.getElementById('email').innerHTML = sessionStorage.getItem('email');
    document.getElementById('username').innerHTML = sessionStorage.getItem('username');
  }else if(getCookie('id') != null) {
    document.getElementById('id').innerHTML = getCookie('id');
    document.getElementById('email').innerHTML = getCookie('email');
    document.getElementById('username').innerHTML = getCookie('username');
  }
}
function logOut() {
  document.cookie = "id=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
  sessionStorage.clear();
  window.location.href = 'login.html';
}
function setCookie(name,value,exdays) {
  let date = new Date();
  date.setTime(date.getTime() + (exdays*24*60*60*1000));
  let expires = "expires=" + date.toGMTString();
  document.cookie = name + "=" + value + "; " + expires;
}
function getCookie(name) {
  if (document.cookie.length > 0) {
    let start = document.cookie.indexOf(name + "=");
    if (start != -1) {
      start = start + name.length + 1;
      let end = document.cookie.indexOf(";", start);
      if (end == -1) end = document.cookie.length;
      return decodeURI(document.cookie.substring(start, end));
    }
  }
  return null;
}
function changeForgetPage(operation) {
  let page = document.getElementsByClassName('layer')[0];
  if(operation == 'open') {
    page.style.display = 'block';
  }else if(operation == 'close') {
    page.style.display = 'none';
  }
}
function getCode() {
  const codeData = {
    email: forgetPass.email.value
  }
  btn = document.getElementById('get-code');
  btn.setAttribute('disabled','disabled');
  let checkRes = checkFormat(codeData.email);
  if(checkRes != false){
    request('http://175.178.51.126:8091/smallA/getCode', 'POST', JSON.stringify(codeData))
    .then(value => {
      if(value.code == 200) {
        alert('获取成功！请查收邮箱！');
      }else{
        alert("获取失败！" + value.msg);
        btn.removeAttribute('disabled');
      }
    })
    .catch(error => {
      console.log(error);
      btn.removeAttribute('disabled');
    })
  }else {
    btn.removeAttribute('disabled');
  }
}
function resetPassword() {
  const resetData = {
    email: forgetPass.email.value,
    newPassword: forgetPass.password.value,
    code: forgetPass.code.value
  }
  let btn = document.getElementById('reset-password');
  btn.setAttribute('disabled','disabled');
  let passwordConfirm = forgetPass.passwordConfirm.value;
  let checkRes = checkFormat(resetData.email, undefined, resetData.newPassword, passwordConfirm);
  if(checkRes != false) {
    request('http://175.178.51.126:8091/smallA/rememberPwd', 'POST', JSON.stringify(resetData))
    .then(value => {
      if(value.code == 200) {
        alert('修改成功！');
        location.reload();
      }else {
        alert('修改失败！' + value.msg);
        btn.removeAttribute('disabled');
      }
    })
    .catch(error => {
      console.log(error);
      btn.removeAttribute('disabled');
    })
  }else {
    btn.removeAttribute('disabled');
  }
}
function switchLoginMethod(content) {
  let emailLogin = document.getElementsByClassName('email-login')[0];
  let usernameLogin = document.getElementsByClassName('username-login')[0];
  let loginMethod = document.getElementsByClassName('login-method')[0].children;
  if(content == 'email') {
    emailLogin.setAttribute('class','email-login active');
    usernameLogin.setAttribute('class','username-login');
    loginMethod[0].style.color = '#00a1e6';
    loginMethod[1].style.color = '#000';
  }else {
    emailLogin.setAttribute('class','email-login');
    usernameLogin.setAttribute('class','username-login active');
    loginMethod[0].style.color = '#000';
    loginMethod[1].style.color = '#00a1e6';
  }

}